﻿#include"stdafx.h"
#ifdef LK_MUTEX_
namespace LK
{
	Mutex& Mutex::GetDefaultRef(){
		static Mutex* pmt;
		if (pmt)return *pmt;
		SpinMutex::GetDefaultRef().Lock();
		if (!pmt) {
			static Mutex mt;
			pmt = &mt;
		}
		SpinMutex::GetDefaultRef().UnLock();
		return *pmt;
	}
	SpinMutex& SpinMutex::GetDefaultRef(){
		static u8 b[sizeof(SpinMutex)];
		return *(SpinMutex*)b;
	}
#ifndef _WINDOWS
	static void* thr_fn(void* p)
	{
		typedef void(*FUN)(void*);
		FUN f = *(FUN*)p;
		void* param = *(void**)(((char*)p) + sizeof(FUN));
		((unsigned char*)p)[sizeof(FUN) + sizeof(void*)] = 0;
		f(param);
		return NULL;
	}
#endif
	/**
	@brief 启动线程的基础API*/
	index_t APIStartThread(void(*fun)(void*), void* param)
	{
#ifdef _WINDOWS
		DWORD dwThreadId;
		::CreateThread(
			0,//NULL,使用默认安全性
			0,//NULL,使用与调用该函数的线程相同的栈空间大小
			  //Thread1,//指定新线程的入口函数名(DWORD WINAPI 声明方法)
			(LPTHREAD_START_ROUTINE)fun,//指定新线程的入口函数名(void 声明方法)
			param,//向新线程传递的参数，不需要传递时设为NULL
			0,//表示新线程立即开始执行
			&dwThreadId);//保存新线程的ID
		return dwThreadId;
#else
		typedef void(*FUN)(void*);
		int err;
		pthread_t ntid;
		unsigned char tmp[sizeof(FUN) + sizeof(void*) + sizeof(unsigned char)];
		tmp[sizeof(FUN) + sizeof(void*)] = 1;
		void *p = tmp;
		*(FUN*)p = fun;
		*(void**)(((unsigned char*)p) + sizeof(FUN)) = param;
		err = pthread_create(&ntid, NULL, thr_fn, p);
		if (err)Throw<PException >("创建线程失败！");
		while (tmp[sizeof(FUN) + sizeof(void*)] != 0)
			Sleep(0);
		return ntid;
#endif
	}
	index_t APIGetThreadID()
	{
#ifdef _WINDOWS
		return (index_t)GetCurrentThreadId();
#else
		return (index_t)pthread_self();
#endif
	}
}
#endif
